package com.yd.app.mapper;

import com.yd.app.mapper.provider.RoleProvider;
import com.yd.domain.searchParam.RoleParam;
import com.yd.domain.sys.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zy
 * @create 2018-03-07 15:24
 * @desc 角色的mapper
 **/
@Repository
public interface RoleMapper {
    /**
     * @Author: zy
     * @Desc:新增角色
     * @Date:  2018/3/7
     **/
    @Insert("INSERT INTO sys_role (`code`,title,des) VALUES(#{role.code},#{role.title},#{role.des})")
    void addRole(@Param("role")Role role);
    /**
     * @Author: zy
     * @Desc:删除角色
     * @Date:  2018/3/7
     */
    @Delete("DELETE FROM sys_role WHERE `code` = #{code}")
    void deleteRoleByCode(@Param("code") String code);
    /**
     * @Author: zy
     * @Desc:角色的修改
     * @Date:  2018/3/7
     */
    @Update("UPDATE sys_role set `code` = #{role.code},title=#{role.title},des=#{role.des} where `code`=#{role.code}")
    void updateRole(@Param("role") Role role);
    /**
     * @Author: zy
     * @Desc: 根据code查询角色
     * @Date:  2018/3/7
     */
    @Select("select `code`,title,des from sys_role where `code` = #{code}")
    Role getRoleByID(@Param("code") String code);
    /**
     * @Author: zy
     * @Desc:角色的分页查询数据
     * @Date:  2018/3/7
     */
    @SelectProvider(method = "searchRoleList",type= RoleProvider.class)
    List<Role> searchRoleList(@Param("roleParam") RoleParam roleParam, @Param("startRow") int startRow, @Param("pageSize") int pageSize);
    /**
     * @Author: zy
     * @Desc:角色查询数量
     * @Date:  2018/3/7
     */
    @SelectProvider(method = "searchRoleCount",type= RoleProvider.class)
    int searchRoleCount(@Param("roleParam") RoleParam roleParam);
    /**
     * 获取角色拥有权限列表数据
     * @param startRow
     * @param pageSize
     * @return
     */
    @SelectProvider(method = "searchPermissionList",type= RoleProvider.class)
    List<Perm> searchPermissionList(@Param("roleId") String roleId, @Param("startRow") int startRow, @Param("pageSize") int pageSize);

    /**
     * 获取角色权拥有限列表数据条数
     * @return
     */
    @SelectProvider(method = "searchPermissionCount",type= RoleProvider.class)
    int searchPermissionCount(@Param("roleId") String roleId);
    /**
     * @Author: zy
     * @Desc: 分页获取角色尚未拥有权限列表数据
     * @Date:  2018/3/7
     */
    @SelectProvider(method = "searchNotHavePermissionList",type= RoleProvider.class)
    List<Perm> searchNotHavePermissionList(@Param("roleId") String roleId, @Param("startRow") int startRow, @Param("pageSize") int pageSize);
    /**
     * @Author: zy
     * @Desc: 获取角色尚未拥有权限列表数据条数
     * @Date:  2018/3/7
     */
    @SelectProvider(method = "searchNotHavePermissionCount",type= RoleProvider.class)
    int searchNotHavePermissionCount(@Param("roleId") String roleId);

    /**
     * @Author: zy
     * @Desc:角色添加权限
     * @Date:  2018/3/7
     */
    @InsertProvider(method = "insertPermissionRole",type = RoleProvider.class)
    void addPermToRole(@Param("list") List<RolePerm> list);
    /**
     * 给角色删除权限
     * @param pr
     */
    @Delete("delete from sys_role_perm where roleId=#{pr.roleId} and permCode=#{pr.permCode} ")
    void deletePermFromRole(@Param("pr")RolePerm pr);
    /**
     * 查询用户角色
     * @param userId
     * @return
     */
    @Select("select sys_role.* from sys_user_role " +
            "left join sys_role on sys_user_role.roleCode=sys_role.code  " +
            "where sys_user_role.userId=#{userId} ")
    List<Role> getUserRole(@Param("userId") String userId);
    /**
     * 通过用户id查询用户权限信息
     * @param userId
     * @return
     */
    @Select("select s_p.* from  sys_role_perm p " +
            "left join sys_user_role r on r.userId=#{userId} " +
            "left join sys_perm s_p on p.permCode=s_p.`code` " +
            "where r.roleCode=p.roleId")
    List<Perm> getUserPerm(@Param("userId") String userId);
    /**
     * 添加用户角色
     * @param userRole
     */
    @Insert("insert into sys_user_role (userId,roleCode) values (#{userRole.userId},#{userRole.roleCode})" +
            " ON DUPLICATE KEY UPDATE roleCode=#{userRole.roleCode} ")
    void saveUserRole(@Param("userRole") UserRole userRole);
    /**
     * 删除用户角色
     * @param userRole
     */
    @Delete("delete from sys_user_role where userId=#{userRole.userId} and roleCode=#{userRole.roleCode} ")
    void deleteUserRole(@Param("userRole") UserRole userRole);

    //根据角色code删除关系信息
    @Delete("delete from sys_user_role where roleCode=#{code} ")
    void deleteUserRoleByRoleCode(@Param("code") String code);
    //根据角色code删除关系信息
    @Delete("delete from  sys_role_perm where roleId=#{code} ")
    void deleteRolePermByRoleCode(@Param("code") String code);

    @Select("select * from sys_role ")
    List<Role> getAllRoleList();

    @Insert("insert into  sys_user_role(userId,roleCode) values (#{ur.userId},#{ur.roleCode}) ON DUPLICATE KEY UPDATE roleCode=#{ur.roleCode}    ")
    void addUserRole(@Param("ur") UserRole ur);
}
